package com.sh169.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.pager.Pager;
import org.nutz.ioc.annotation.InjectName;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.sh169.actionform.ProductForm;
import com.sh169.beans.PageForm;
import com.sh169.pojos.Article;
import com.sh169.pojos.Brand;
import com.sh169.pojos.Classify;
import com.sh169.pojos.Pinpai;
import com.sh169.pojos.Product;
import com.sh169.pojos.User;
@IocBean(name="frontHomeController")
@InjectName("frontHomeController")
public class FrontHomeController {
	private static Log log = Logs.getLog(FrontHomeController.class);
	@Inject("refer:dao")
	private Dao dao;
	public void setDao(Dao dao) {
		this.dao = dao;
	}
	@At("/product_*")
	@Ok("tiles:front.product")
	public ProductForm lookProduct(int productid){
		
		ProductForm pf = new ProductForm();
		if(productid<1) return null;
		Product product = dao.fetch(Product.class,productid);
		if(product==null) return null;
		dao.fetchLinks(product, "vendor");
		dao.fetchLinks(product, "classify");
		dao.fetchLinks(product, "brand");
		if(product.getVendor()==null || product.getVendor().getVendorState()!= User.STATE_SUCCESS)
			return null;
		User user = dao.fetch(User.class,product.getVendor().getUserid());
		pf.setProduct(product);
		pf.setUser(user);
		pf.setVendor(product.getVendor());
		return pf;
	}
	@At("/article_*")
	@Ok("tiles:front.article")
	public Article lookarticle(int aid){
		
		if(aid<1) return null;
		Article a = dao.fetch(Article.class,aid);
		if(a==null) return null;
		return a;
	}
	@At("/search")
	@Ok("tiles:front.search")
	public PageForm<Product> searchProduct(@Param("name")String productName,@Param("pageNum")int pageNum,
			                        @Param("brandid")int brandid,@Param("classifyid")int classifyid,
			                        @Param("price")String price,HttpServletRequest req){
		Pager page = dao.createPager(pageNum<1?1:pageNum, 12);
		PageForm<Product> pf = new PageForm<Product>();
		List<Product> productList = null;
		StringBuffer cndSql = new StringBuffer();
		cndSql.append("1=1 and state=").append(Product.STATE_ENABLE);
		if(!Strings.isBlank(productName)){
			cndSql.append(" and productName like '%"+productName+"%'");
			req.setAttribute("productName", productName);
		}
		if(brandid>0){
			cndSql.append(" and brandid=").append(brandid);
			req.setAttribute("brandid", brandid);
		}
		if(classifyid>0){
			Classify cly = dao.fetch(Classify.class,classifyid);
			if(cly !=null){
				req.setAttribute("classifyid", classifyid);
				if(cly.getParentid()!=0){
					cndSql.append(" and classifyid=").append(cly.getId());
				}else{
					List<Classify> cs = dao.query(Classify.class, Cnd.where("parentid","=",cly.getId()), null);
					if(cs.size()>0){
						cndSql.append(" and classifyid in (");
						boolean first = true;
						for(Classify c:cs){
							if(first){
								cndSql.append(c.getId());
								first = false;
							}else{
								cndSql.append(",").append(c.getId());
							}
						}
						cndSql.append(")");
					}
				}
			}
		}
		if(!Strings.isEmpty(price)){
			if("0X50".equals(price)){
				cndSql.append(" and price <50");
			}else if("50X150".equals(price)){
				cndSql.append(" and price>= 50 and price <150");
			}else if("150X250".equals(price)){
				cndSql.append(" and price>= 150 and price <250");
			}else if("250X350".equals(price)){
				cndSql.append(" and price>= 250 and price <350");
			}else if("350X500".equals(price)){
				cndSql.append(" and price>= 350 and price <500");
			}else if("500X800".equals(price)){
				cndSql.append(" and price>= 500 and price <800");
			}else if("800X1500".equals(price)){
				cndSql.append(" and price>= 800 and price <1500");
			}else if("1500X0".equals(price)){
				cndSql.append(" and price >=1500");
			}
			req.setAttribute("price", price);
		}
		cndSql.append(" order by price");
		List<Brand> brands = dao.query(Brand.class, null, null);
		req.setAttribute("brands",brands);
		log.info(cndSql.toString());
		productList = dao.query(Product.class, Cnd.wrap(cndSql.toString()), page);
		page.setRecordCount(dao.count(Product.class,Cnd.wrap(cndSql.toString())));
		pf.setPage(page);
		pf.setResultList(productList);
		return pf;
	}
	@At("/pinpai/introduction")
	@Ok("tiles:front.pinpai.introduction")
	public Pinpai pinpaiIntroduction(@Param("id")int id,HttpServletRequest req){
		if(id<1) return null;
		req.setAttribute("id", id);
		Pinpai pp = dao.fetch(Pinpai.class,id);
		if(pp == null) return null;
		return pp;
	} 
	@At("/pinpai/products")
	@Ok("tiles:front.pinpai.products")
	public PageForm<Product> pinpaiProducts(@Param("pageNum")int num,@Param("id")int pinpaiid,HttpServletRequest req){
		if(pinpaiid==0) return null;
		req.setAttribute("id", pinpaiid);
		if(num<1) num =1;
		PageForm<Product> pf = new PageForm<Product>();
		Pager page = dao.createPager(num, 20);
		page.setRecordCount(dao.count(Product.class,Cnd.wrap("pinpaiid="+pinpaiid+" and state="+Product.STATE_ENABLE)));
		if(num>(page.getRecordCount()%20==0?page.getRecordCount()%20:(page.getRecordCount()%20+1))){
			num = 1;
		}
		page.setPageNumber(num);
		List<Product> products = dao.query(Product.class, Cnd.wrap("pinpaiid="+pinpaiid+" and state="+Product.STATE_ENABLE), page);
		pf.setResultList(products);
		pf.setPage(page);
		return pf;
	} 
	
}
